package com.atguigu.ting.realtime.util;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.HashSet;
import java.util.Set;

/**
 * @Author lzc
 * @Date 2023/3/11 11:42
 */
public class AtguiguUtil {
    private final static DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private final static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    
    public static String tsToDate(Long ts) {
        return dateFormatter.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(ts), ZoneId.systemDefault()));
    }
    
    public static String tsToDateTime(long ts) {
        return dateTimeFormatter.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(ts), ZoneId.systemDefault()));
    }
    
    
    public static Long dateToTs(String date) {
        return dateTimeToTs(date + " 00:00:00");
    }
    
    public static Long dateTimeToTs(String dateTime) {
        return LocalDateTime.parse(dateTime, dateTimeFormatter).toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }
    
    
    
    public static Set<String> ikSpilt(String s) {
        HashSet<String> result = new HashSet<>();
        // 内存流
        // 字符串 -> 字符输入流
        StringReader reader = new StringReader(s);
        IKSegmenter segmenter = new IKSegmenter(reader, true);// 创建一个IKSegmenter
        Lexeme next = null;
        try {
            next = segmenter.next();
            while (next != null) {
                String kw = next.getLexemeText();
                result.add(kw);
                next = segmenter.next();
            }
            
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        
        return result;
    }
    
    public static void main(String[] args) throws IOException {
        System.out.println(dateToTs("2022-02-02"));
    }


    
}
